package servlet;


import entity.Customer;
import service.CustomerService;
import service.Impl.CustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/Customer")
public class CustomerServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method == null) {
            method = "findAll_customer";
        }
        switch (method) {
            // 查询所有客户信息
            case "findAll_customer":
                CustomerService showCustomerService = new CustomerServiceImpl();
                ArrayList<Customer> customerList = null;
                customerList = showCustomerService.getCustomerList();
                req.setAttribute("CustomerList", customerList);
                req.getRequestDispatcher("jsp/customer.jsp").forward(req, resp);
                break;
            // 接受添加请求，跳转到添加客户界面
            case "add":
                resp.sendRedirect("jsp/addCustomer.jsp");
                break;
            //添加
            case "addCustomer":
                Integer cid = Integer.valueOf(req.getParameter("customerId"));
                String name = req.getParameter("customerName");
                Integer age = Integer.valueOf(req.getParameter("age"));
                String job = req.getParameter("job");
                String addr = req.getParameter("addr");
                String phone = req.getParameter("phone");
                String wechat = req.getParameter("wechat");
                String mail = req.getParameter("mail");
                Customer customer = new Customer(cid,name,age,job,addr,phone,wechat,mail);
                CustomerService customerService1 = new CustomerServiceImpl();
                //判断是否添加成功
                boolean isAdd = customerService1.addCustomer(customer);
                // 如果成功添加，返回客户列表界面
                if (isAdd) {
                    resp.sendRedirect("Customer");
                }else { // 修改失败，跳转到错误界面
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;
            // 接受更新请求，跳转到更改界面
            case "update":
                Integer customerId = Integer.valueOf(req.getParameter("customerId"));
                CustomerService customerService = new CustomerServiceImpl();
                Customer customer1 = customerService.findById(customerId);
                req.setAttribute("customer", customer1);
                req.getRequestDispatcher("jsp/updateCustomer.jsp").forward(req,resp);
                break;
            // 修改某个客户信息
            case "updateCustomer":
                // 获取客户信息
                cid = Integer.valueOf(req.getParameter("customerId"));
                name = req.getParameter("customerName");
                age = Integer.valueOf(req.getParameter("age"));
                job = req.getParameter("job");
                addr = req.getParameter("addr");
                phone = req.getParameter("phone");
                wechat = req.getParameter("wechat");
                mail = req.getParameter("mail");
                Customer customer2 = new Customer(cid,name,age,job,addr,phone,wechat,mail);
                CustomerService customerService2 = new CustomerServiceImpl();
                // 判断是否修改成功
                boolean isUpdate = customerService2.updateCustomer(customer2);
                // 如果成功修改，返回客户列表界面
                if (isUpdate) {
                    resp.sendRedirect("Customer");
                }else { // 修改失败，跳转到错误界面
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;
            // 根据客户编号删除客户信息
            case "delete":
                Integer customerId2 = Integer.valueOf(req.getParameter("customerId"));
                CustomerService customerService3 = new CustomerServiceImpl();
                boolean isDelete = customerService3.deleteCustomer(customerId2);
                if (isDelete) {
                    resp.sendRedirect("Customer");
                } else {
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;


            default:
                break;
        }

    }

}
